#include "S_2_AirConditioner_doSet.h"
#include "string.h"
#include "mi_iid_define.h"
#include "spec.h"
#include "S_2_AirConditioner_doChange.h"
#include "main.h"
#include "section1_set_func.h"


static void P_2_1_On_doSet(property_operation_t *o)
{
	// 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
	if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
	{
			o->code = OPERATION_ERROR_VALUE;
			return;
	}

	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON && o->value.data.boolean.value == true)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
	
    // TODO: 执行写操作: o->value.data.boolean.value;
		
	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
		
    CloudSetting(SWITCH_SET,o->value.data.boolean.value,0);	
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_1_On_doChange_notify(o->value.data.boolean.value);

    return;
}

static void P_2_2_Mode_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // 简单判断数据取值范围是否合法，如果不合法，返回代码: OPERATION_ERROR_VALUE
    if (o->value.data.number.value.integerValue < 2 || o->value.data.number.value.integerValue > 6)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSwitchState() == SWITCH_OFF)  //关机状态下发模式指令，整机答复-5000
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.number.value.integerValue;
	LOG_INFO("%s, %ld\r\n",__func__,o->value.data.number.value.integerValue);
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
		
		CloudSetting(MODE_SET,o->value.data.number.value.integerValue,0);
		
    return;
}

static void P_2_4_TargetTemperature_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // 判断数据取值范围是否合法，如果不合法，返回代码: OPERATION_ERROR_VALUE
    if (o->value.data.number.value.floatValue < 16.0 || o->value.data.number.value.floatValue > 31.0)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSwitchState() == SWITCH_OFF)  //"1、关机状态下发设定温度，固件答复-5000
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(GetAirMode() == FANMODE)
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.number.value.floatValue;
	LOG_INFO("%s, %.2f\r\n",__func__,o->value.data.number.value.floatValue);
	

    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
  //温度设置	 
	CloudSetting(TEMPERATURE_SET,0,o->value.data.number.value.floatValue);	
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_4_TargetTemperature_doChange_notify(o->value.data.number.value.floatValue);

    return;
}

static void P_2_7_Eco_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSwitchState() == SWITCH_OFF)  //"1、关机状态下发节能模式，固件答复-5000
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(GetAirMode() != COLDMODE && GetAirMode() != HOTMODE)
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	//单开离子净化下发节能回复错误
	if((GetAirMode() == EMPTYMODE) && (GetPurifyModeState() == SWITCH_ON))
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
		
	CloudSetting(ECO_SET,o->value.data.boolean.value,0);	
	  
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_7_Eco_doChange_notify(o->value.data.boolean.value);

    return;
}

static void P_2_9_Heater_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSwitchState() == SWITCH_OFF)  //"1、关机状态下发辅热功能，固件答复-5000
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(GetAirMode() != HOTMODE)
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	//单开离子净化下发节能回复错误
	if((GetAirMode() == EMPTYMODE) && (GetPurifyModeState() == SWITCH_ON))
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
	
	CloudSetting(HEATER_SET,o->value.data.boolean.value,0);	

    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_9_Heater_doChange_notify(o->value.data.boolean.value);

    return;
}

static void P_2_10_Dryer_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSwitchState() == SWITCH_OFF) 
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(GetAirMode() == HOTMODE || GetAirMode() == FANMODE)
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	//单开离子净化下发节能回复错误
	if((GetAirMode() == EMPTYMODE) && (GetPurifyModeState() == SWITCH_ON))
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
  
	A00_2_10_Set(o->value.data.boolean.value);		
//	BeepStart();	
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_10_Dryer_doChange_notify(o->value.data.boolean.value);

    return;
}

static void P_2_11_SleepMode_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSwitchState() == SWITCH_OFF) 
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(GetAirMode() != HOTMODE && GetAirMode() != COLDMODE)
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	//单开离子净化下发节能回复错误
	if((GetAirMode() == EMPTYMODE) && (GetPurifyModeState() == SWITCH_ON))
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
		
	CloudSetting(SLEEP_SET,o->value.data.boolean.value,0);	
		
    
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_11_SleepMode_doChange_notify(o->value.data.boolean.value);

    return;
}

//static void P_2_14_TargetHumidity_doSet(property_operation_t *o)
//{
//    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
//    if (o->value.format != PROPERTY_FORMAT_NUMBER)
//    {
//        o->code = OPERATION_ERROR_VALUE;
//        return;
//    }

//    // 判断数据取值范围是否合法，如果不合法，返回代码: OPERATION_ERROR_VALUE
//    if (o->value.data.number.value.integerValue < 0 || o->value.data.number.value.integerValue > 100)
//    {
//        o->code = OPERATION_ERROR_VALUE;
//        return;
//    }

//    // TODO: 执行写操作: o->value.data.number.value.integerValue;

//	LOG_INFO("%s, %ld\r\n",__func__,o->value.data.number.value.integerValue);
//	
//    // 如果成功，返回代码: OPERATION_OK
//    o->code = 0;
//		
//		
//    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
////    P_2_14_TargetHumidity_doChange_notify(o->value.data.number.value.integerValue);

//    return;
//}

static void P_2_17_PurifySwitch_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // 简单判断数据取值范围是否合法，如果不合法，返回代码: OPERATION_ERROR_VALUE
    if (o->value.data.number.value.integerValue < 0 || o->value.data.number.value.integerValue > 1)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(Get3DPanelFuncFlag() == SH_RESET)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;		
	}		
			
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
	
	
    // TODO: 执行写操作: o->value.data.number.value.integerValue;

	LOG_INFO("%s, %ld\r\n",__func__,o->value.data.number.value.integerValue);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
		//净化模式设置
	CloudSetting(PURIFY_SET,o->value.data.number.value.integerValue,0);
			
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_17_PurifySwitch_doChange_notify(o->value.data.number.value.integerValue);

    return;
}

static void P_2_18_ListSoftWind_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // 简单判断数据取值范围是否合法，如果不合法，返回代码: OPERATION_ERROR_VALUE
    if (o->value.data.number.value.integerValue < 0 || o->value.data.number.value.integerValue > 1)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSwitchState() == SWITCH_OFF) 
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}

	if(Get3DPanelFuncFlag() == SH_RESET)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;		
	}		
	
	if(GetAirMode() != COLDMODE)
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	//单开离子净化下发节能回复错误
	if((GetAirMode() == EMPTYMODE) && (GetPurifyModeState() == SWITCH_ON))
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.number.value.integerValue;

	LOG_INFO("%s, %ld\r\n",__func__,o->value.data.number.value.integerValue);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
	CloudSetting(SOFTWIND_SET,o->value.data.number.value.integerValue,0);	

//	BeepStart();
		
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_18_ListSoftWind_doChange_notify(o->value.data.number.value.integerValue);

    return;
}

static void P_2_19_FavoriteOn_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
    
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_19_FavoriteOn_doChange_notify(o->value.data.boolean.value);
	A03_2_19_Set(o->value.data.boolean.value);
//	BeepStart();	
		
    return;
}

static void P_2_20_FavoriteType_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // 简单判断数据取值范围是否合法，如果不合法，返回代码: OPERATION_ERROR_VALUE
    if (o->value.data.number.value.integerValue < 0 || o->value.data.number.value.integerValue > 1)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.number.value.integerValue;

	LOG_INFO("%s, %ld\r\n",__func__,o->value.data.number.value.integerValue);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;
    
	A18_2_20_Set(o->value.data.number.value.integerValue);
//	BeepStart();		
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_2_20_FavoriteType_doChange_notify(o->value.data.number.value.integerValue);

    return;
}

static void P_2_28_PeoPleApproachSoftWind_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

	if(AirGetSwitchState() == SWITCH_OFF) 
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(GetAirMode() != COLDMODE)
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(Get3DPanelFuncFlag() == SH_RESET)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;		
	}			
	
	//单开离子净化下发节能回复错误
	if((GetAirMode() == EMPTYMODE) && (GetPurifyModeState() == SWITCH_ON))
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = 0;	
		
	A07_2_28_Set(o->value.data.boolean.value);
//	BeepStart();
    return;
}

//static void P_2_34_FormaldehydeRemoval_doSet(property_operation_t *o)
//{
//	// 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
//	if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
//	{
//			o->code = OPERATION_ERROR_VALUE;
//			return;
//	}
//    // TODO: 执行写操作: o->value.data.boolean.value;
//	
//	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
//	
//	// 如果成功，返回代码: OPERATION_OK
//	o->code = OPERATION_OK;
//	
//	//无感除醛
//	A07_2_34_Set(o->value.data.boolean.value);
//	BeepStart();
//	
//	return;
//}


void S_2_AirConditioner_doSet(property_operation_t *o)
{
    switch (o->piid)
    {
        case PIID_2_1_On:
            P_2_1_On_doSet(o);
            break;

        case PIID_2_2_Mode:
            P_2_2_Mode_doSet(o);
            break;

        case PIID_2_4_TargetTemperature:
            P_2_4_TargetTemperature_doSet(o);
            break;

        case PIID_2_7_Eco:
            P_2_7_Eco_doSet(o);
            break;

        case PIID_2_9_Heater:
            P_2_9_Heater_doSet(o);
            break;

        case PIID_2_10_Dryer:
            P_2_10_Dryer_doSet(o);
            break;

        case PIID_2_11_SleepMode:
            P_2_11_SleepMode_doSet(o);
            break;

//        case PIID_2_14_TargetHumidity:
//            P_2_14_TargetHumidity_doSet(o);
//            break;

        case PIID_2_17_PurifySwitch:
            P_2_17_PurifySwitch_doSet(o);
            break;

        case PIID_2_18_ListSoftWind:
            P_2_18_ListSoftWind_doSet(o);
            break;

        case PIID_2_19_FavoriteOn:
            P_2_19_FavoriteOn_doSet(o);
            break;

        case PIID_2_20_FavoriteType:
            P_2_20_FavoriteType_doSet(o);
            break;

				case PIID_2_28_PeoPleApproachSoftWind:
					P_2_28_PeoPleApproachSoftWind_doSet(o);
					break;
				
//				case PIID_2_34_FormaldehydeRemoval:
//					P_2_34_FormaldehydeRemoval_doSet(o);
//					break;
				
        default:
            o->code = OPERATION_ERROR_VALUE;
            break;
    }
}
